package com.lucky.common.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lucky.common.base.BaseService;
import com.lucky.common.mapper.LoginMapper;
import com.lucky.common.pojo.po.LoginPO;
import com.lucky.common.pojo.po.UserPO;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginService extends BaseService<LoginMapper,LoginPO> {

	public Logger log = LoggerFactory.getLogger(LoginService.class);
	
	@Resource
	LoginMapper mLoginMapper;

	@Resource
	OrgService orgService;
	@Resource
	UserService userService;


	public String loginInit(HttpSession session, ModelMap model, UserPO user, String password, String loginType, String loginToken, String sessionId) {

		List<String> roleSet = userService.queryRoles(user.getUsername());
		if(null==roleSet || roleSet.isEmpty()) return "/error/notLogin";
		if(!roleSet.contains("app_bank") && !roleSet.contains("app_hos")) return "/error/notLogin";

//		session.kickOutUser(user.getUserName());
		loginTest(user.getUsername(), password);
//		Subject subject = session.getSubject();
//		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), password);
//		subject.login(token);

		//登录认证成功
//		boolean result= subject.isAuthenticated();
//		log.info("认证结果：{}", subject.isAuthenticated());
		boolean result = true;
		if(result){
			user.setLogonTime(new Date());
			userService.update(Wrappers.update(user));

			roleSet.remove(null);
			//去掉静态对象的缓存改为session存储，便于过期后内存回收
			Set<String> roleSet1 = new HashSet<>(roleSet);
			//添加用户角色名称
			List<String> roleNameSet = userService.queryRoleNames(user.getUsername());
			String roleNames = StringUtils.join(roleNameSet, ",");

			//用户所有的权限存入缓存
			Set<String> permSet = new HashSet<>();
//			roleSet.forEach(roleCode->permSet.addAll(session.getRolePerms().get(roleCode)));
//
//			//对外系统权限树
//			if(user.getUserLevel().equals(AppConst.OUT)){
//				if(session.getRightTree(user.getUsername())==null || session.getRightTree(user.getUsername()).isEmpty()){
//					List<PrivilegeVO> navTreeList = new ArrayList<PrivilegeVO>();
//					if(roleSet.contains("app_hos")){
//						navTreeList.addAll(permService.queryPrivList2(user.getUserName(), "410")) ;
//					}else if(roleSet.contains("app_bank")){
//						navTreeList.addAll(permService.queryPrivList2(user.getUserName(), "400")) ;
//					}
//					session.setRightTree(user.getUsername(), navTreeList);
//				}
//			}


			session.setAttribute("role", roleSet1);
			session.setAttribute("userRoleNames", roleNames);
			session.setAttribute("userPerms", permSet);
			session.setAttribute("login_type", loginType);
			session.setAttribute("rememberMe", true);
			session.setAttribute("userLevel", user.getUserLevel());


			session.setAttribute("sessionId", sessionId);
			session.setAttribute("login_token", loginToken);

			//设置session超时时间，单位：秒
//			request.getSession().setMaxInactiveInterval(timeOut);
		}


		return "/login/dash";
	}


	public void loginTest(String usercode, String password) {
//		SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
//		simpleAccountRealm.addAccount(usercode, password);
//		DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//		defaultSecurityManager.setRealm(simpleAccountRealm);
//		SecurityUtils.setSecurityManager(defaultSecurityManager);
	}





}
